package scau.redi.social_platform.controller.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scau.redi.social_platform.common.staticCode.Const;
import scau.redi.social_platform.common.staticCode.ResponseCode;
import scau.redi.social_platform.common.util.ResponseUtil;
import scau.redi.social_platform.common.util.SessionContextUtil;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.service.LoginService;
import scau.redi.social_platform.service.UserService;
import scau.redi.social_platform.service.impl.LoginServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: Redi
 * @Date: 2022/5/8 16:45
 * @Description:
 */

@RestController
@Api(value = "login",description = "登入/登出管理")
public class LoginController {

    @Autowired
    LoginService loginService;

    // 创建实例
    SessionContextUtil sessionUtil = SessionContextUtil.getInstance();


//    @PostMapping("/login.do")
//    @ApiOperation("登录")
//    public Map<String, Object> login(@RequestParam(value = "username", required = true)String username,
//                                     @RequestParam(value = "password", required = true)String password,
//                                     HttpSession session){
//        UserInfo userInfo = loginService.loginByUsernameAndPassword(username, password);
//        if(null != userInfo){
//            session.setAttribute(Const.CURRENT_USER,userInfo);
//            return ResponseUtil.success("登录成功",ResponseCode.SUCCESS.getCode(),userInfo);
//        }
//        return ResponseUtil.success("登录失败！用户名与密码不正确",ResponseCode.ERROR.getCode(),userInfo);
//    }

//    @GetMapping("/logout.do")
//    @ApiOperation("登出")
//    public Map<String, Object> logout(HttpSession session){
//        session.removeAttribute(Const.CURRENT_USER);
//        return ResponseUtil.success("登出成功");
//    }

    //-------------------------------------------------new solution start

    @PostMapping("/login.do")
    @ApiOperation("登录")
    public Map<String, Object> login(@RequestParam(value = "username", required = true)String username,
                                     @RequestParam(value = "password", required = true)String password,
                                     HttpSession session)
    {
        UserInfo userInfo = loginService.loginByUsernameAndPassword(username, password);
        if(null != userInfo){
            session.setAttribute(Const.CURRENT_USER,userInfo);
            sessionUtil.AddSession(session);

            // 返回sessionId 和 用户信息
            List responseList = new ArrayList<Object>();
            responseList.add(session.getId());
            responseList.add(userInfo);
            return ResponseUtil.success("登录成功",ResponseCode.SUCCESS.getCode(),responseList);
        }
        return ResponseUtil.success("登录失败！用户名与密码不正确",ResponseCode.ERROR.getCode(),userInfo);
    }

    @GetMapping("/logout.do")
    @ApiOperation("登出")
    public Map<String, Object> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        sessionUtil.DelSession(session);

        return ResponseUtil.success("登出成功");
    }

    //-------------------------------------------------new solution end

    @PostMapping("/register.do")
    @ApiOperation("注册")
    public Map<String, Object> register(@RequestParam(value = "username", required = true)String username,
                                        @RequestParam(value = "password", required = true)String password,
                                        HttpSession session){
        // 检查用户名密码是否为空
        if (StringUtils.isAllBlank(username,password)){
            return ResponseUtil.fail("注册失败,用户名与密码不可为空！");
        }
        // 检查用户名是否已占用
        if(null != loginService.findByUsername(username)){
            return ResponseUtil.fail("注册失败,该用户名已被占用！");
        }
        // 插入新用户
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo.setRoleId(Const.Role.ROLE_USER);
        userInfo.setStatusId(Const.UserStatus.STATUS_NORMAL);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());

        int registerCode = loginService.registerByUsernameAndPassword(userInfo);
        if(registerCode > 0){
            return ResponseUtil.success("注册成功", ResponseCode.SUCCESS.getCode(),loginService.findByUserId(registerCode));
        }
        return ResponseUtil.fail("注册失败！",userInfo);
    }


}
